package com.lilinling.yinzhi.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.lilinling.yinzhi.user.entity.UserEntity;
import com.lilinling.yinzhi.user.exception.MakerExsitException;
import com.lilinling.yinzhi.user.exception.MakerNameExsitException;
import com.lilinling.yinzhi.user.vo.MakerInfoVo;
import com.lilinling.yinzhi.user.vo.MakerOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lilinling.common.utils.PageUtils;
import com.lilinling.common.utils.Query;

import com.lilinling.yinzhi.user.dao.MakersDao;
import com.lilinling.yinzhi.user.entity.MakersEntity;
import com.lilinling.yinzhi.user.service.MakersService;
import org.springframework.transaction.annotation.Transactional;


@Service("makersService")
public class MakersServiceImpl extends ServiceImpl<MakersDao, MakersEntity> implements MakersService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MakersEntity> page = this.page(
                new Query<MakersEntity>().getPage(params),
                new QueryWrapper<MakersEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存
     * @param makerInfoVo
     */
    @Transactional
    @Override
    public void saveMakerInfo(Long userId,MakerInfoVo makerInfoVo) {
        MakersEntity makersEntity = new MakersEntity();

        checkUserIdUnique(userId);
        checkMakerNameUnique(makerInfoVo.getMakerName());

        BeanUtil.copyProperties(makerInfoVo,makersEntity);
        makersEntity.setUserId(userId);
        makersEntity.setStatus(1);
        makersEntity.setCreateTime(new Date());
        makersEntity.setUpdateTime(new Date());
        this.baseMapper.insert(makersEntity);

    }



    /**
     * 根据制作人id查询制作人信息
     * @param makerId
     * @return
     */
    @Override
    public MakerOrderVo getMakerOrderVoByMakerId(Long makerId) {
        MakersEntity makersEntity = baseMapper.selectById(makerId);
        MakerOrderVo makerOrderVo = new MakerOrderVo();
        BeanUtil.copyProperties(makersEntity,makerOrderVo);
        return makerOrderVo;
    }

    /**
     * 用户id验证制作人是否存在
     */
    @Override
    public void checkUserIdUnique(Long userId) throws MakerExsitException {
        MakersDao makersDao = this.baseMapper;
        Integer count = makersDao.selectCount(new QueryWrapper<MakersEntity>().eq("user_id", userId));
        if(count > 0) {
            throw new MakerExsitException();
        }
    }

    /**
     * 制作人名称验证
     */
    @Override
    public void checkMakerNameUnique(String makerName) throws MakerNameExsitException {
        MakersDao makersDao = this.baseMapper;
        Integer count = makersDao.selectCount(new QueryWrapper<MakersEntity>().eq("maker_name", makerName));
        if(count > 0) {
            throw new MakerNameExsitException();
        }
    }
}